package ru.spies.forms;

import javax.servlet.http.Cookie;

import ru.spies.content.DBFunctionalContent;
import ru.spies.dbfunctional.ICallFunction;
import ru.spies.dbwork.DBWork;
import ru.spies.entities.Role;

public class DBFunctionalForm extends FormCreator {
	
	private static DBFunctionalForm statThis;
	
	private static final String FUNCTIONS_NAMES []= {
		"Показать пользователей",
		"Изменить пользователя", "Удалить пользователя",
		"Топ N агентов", "Существует ли такая страна?",
		"Получить документы по типу важности", "Самый многочисленный отдел"
	};
	
	private static final String UPDATE_USER="UpdateUserGetStep";
	private static final String UPDATE_USER_IN_DB="UpdateUserInDB";
	private static final String DELETE_USER="DeleteUser";
	private static final String SHOW_USERS="ShowUsers";
	private static final String SP_CALLER="SPCaller";
	private static final String IS_COUNTRY_EXISTS="IsCountryExists";
	
	private static final String TOP_AGENTS_HEADER="Сколько агентов входит в TOP?";
	private static final String IS_COUNTRY_EXISTS_HEADER="Существует ли такая страна?";
	
	private ICallFunction caller=null;

	private DBFunctionalForm() {
		setCookieName(LastCookies.COOKIE_NAME_FORM_FOR_WORK);
	}
	
	public static DBFunctionalForm getInstance(){
		if (statThis==null){
			statThis = new DBFunctionalForm();
		}
		return statThis;
	}

	@Override
	protected ICookie childCookieWork(Cookie cookie) {
		if (Integer.valueOf(cookie.getValue())==EDBFunctional.CHANGE_USER.ordinal()){
			return EDBFunctional.CHANGE_USER;
		}
		if (Integer.valueOf(cookie.getValue())==EDBFunctional.DELETE_USER.ordinal()){
			return EDBFunctional.DELETE_USER;
		}
		if (Integer.valueOf(cookie.getValue())==EDBFunctional.GET_DOCUMENTS_BY_IMPORTANCE.ordinal()){
			return EDBFunctional.GET_DOCUMENTS_BY_IMPORTANCE;
		}
		if (Integer.valueOf(cookie.getValue())==EDBFunctional.GET_POPULAR_DEPARTMENT.ordinal()){
			return EDBFunctional.GET_POPULAR_DEPARTMENT;
		}
		if (Integer.valueOf(cookie.getValue())==EDBFunctional.IS_COUNTRY_EXISTS.ordinal()){
			return EDBFunctional.IS_COUNTRY_EXISTS;
		}
		if (Integer.valueOf(cookie.getValue())==EDBFunctional.TOP_AGENTS.ordinal()){
			return EDBFunctional.TOP_AGENTS;
		}
		if (Integer.valueOf(cookie.getValue())==EDBFunctional.SHOW_USERS.ordinal()){
			return EDBFunctional.SHOW_USERS;
		}	
		return null;
	}

	@Override
	public String createThisForm() {
		return createForm(DBFunctionalContent.getInstance().formDBFunctionalContent(FUNCTIONS_NAMES));
	}

	@Override
	protected String workWithCookieValue(ICookie cook) {
		EDBFunctional func = (EDBFunctional) cook;
		LastCookies.getInstance().setLastDBFunction(func);
		switch (func) {
		case SHOW_USERS:
			return createFormWithPostMethod(DBFunctionalContent.getInstance().acceptContent(),SHOW_USERS);
		case CHANGE_USER:
			return createFormWithPostMethod(DBFunctionalContent.getInstance().getUserContent(),UPDATE_USER);
		case DELETE_USER:
			return createFormWithPostMethod(DBFunctionalContent.getInstance().getUserContent(),DELETE_USER);
		case GET_DOCUMENTS_BY_IMPORTANCE:
			caller=DBWork.getInstance().getImportantDocuments();
			return createFormWithPostMethod(DBFunctionalContent.getInstance().getImportanceContent(),SP_CALLER);
		case GET_POPULAR_DEPARTMENT:
			caller=DBWork.getInstance().getPopularDepartment();
			return createFormWithPostMethod(DBFunctionalContent.getInstance().acceptContent(),SP_CALLER);
		case IS_COUNTRY_EXISTS:
			return createFormWithPostMethod(DBFunctionalContent.getInstance().getSPContent(IS_COUNTRY_EXISTS_HEADER),IS_COUNTRY_EXISTS);
		case TOP_AGENTS:
			caller=DBWork.getInstance().getTopAgents();
			return createFormWithPostMethod(DBFunctionalContent.getInstance().getSPContent(TOP_AGENTS_HEADER),SP_CALLER);
		default:
			return createForm(SOME_ERRORS);
		}	
	}
	
	public String createUpdateUserForm(Role userRole){
		return createFormWithPostMethod(DBFunctionalContent.getInstance().getUpdateContent(userRole),UPDATE_USER_IN_DB );
	}
	
	public String createShowUserForm(){
		return DBFunctionalContent.getInstance().getShowUsersContent();
	}
	
	public ICallFunction getLastCaller(){
		return caller;
	}
	
	public String createGoodOrBadCountryForm(boolean good){
		return createForm(DBFunctionalContent.getInstance().getGoodOrBadCountry(good));
	}

}
